package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

	@Autowired
	private EmployeeMapper employeeMapper;
	@Override
	public void deleteByPrimaryKey(Long id) {
		
		employeeMapper.deleteRelation(id);
		employeeMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void insert(Employee entity, Long[] ids) {

		Md5Hash md5Hash = new Md5Hash(entity.getPassword(),entity.getName(),2);
		entity.setPassword(md5Hash.toString());
		employeeMapper.insert(entity);
		if(ids != null){
			for (Long id : ids) {
				employeeMapper.insertRelation(entity.getId(),id);
			}

		}

	}

	@Override
	public Employee selectByPrimaryKey(Long id) {
		
		return employeeMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Employee> selectAll() {
		
		return employeeMapper.selectAll();
	}

	@Override
	public void updateByPrimaryKey(Employee entity,Long[] ids) {
		
		employeeMapper.updateByPrimaryKey(entity);
		employeeMapper.deleteRelation(entity.getId());
		if(ids != null){
			for (Long id : ids) {
				employeeMapper.insertRelation(entity.getId(),id);
			}
		}
	}

	@Override
	public PageInfo query(EmployeeQueryObject qo) {
		//查询总记录数
		PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
		List<Employee> data = employeeMapper.queryForList(qo);
		return new PageInfo(data);
	}



	public void batchDelete(Long[] ids) {
		for (Long id : ids) {
			employeeMapper.deleteRelation(id);
		}
		employeeMapper.batchDelete(ids);
	}

	public String selectByName(String name) {
		return employeeMapper.selectByName(name);
	}

}
